package com.oncliedisk.administrator.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.oncliedisk.administrator.Base.BaseActivity;
import com.oncliedisk.administrator.DB.Dao;
import com.oncliedisk.administrator.adapter.FileAdapter;
import com.oncliedisk.administrator.model.EntryModel;

/**
 * Created by Administrator on 2015/12/11.
 */
public class ShowFolder extends BaseActivity {
    private String indexPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isSearch = true;
        initData();
    }

    private void initData() {
        EntryModel model = (EntryModel) getIntent().getSerializableExtra("data");
        setTitle(model.getFileName());
        initAdapter(Dao.getInstance(this).getAllFile("parent_url=?", new String[]{model.path + "/"}));
        listView.setAdapter(adapter);
        indexPath = model.path;
        currPath = model.path;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_BACK) {
            if (isMulChoice) {
                //如果在长按状态下按了返回，则退出长按状态
                cancelLongClick();
                return false;
            }
            if(!isSearch){
                finish();
                return false;
            }
            Toast.makeText(getActivity(), "currpath: "+currPath+"indexpath: "+indexPath,Toast.LENGTH_LONG).show();
            if (currPath.equals(indexPath)) {
                //如果已经是目录了，就不退了
                finish();
                return false;
            }
            //把当前路径分割，获取当前目录的上一个目录的ID
            if (currPath.contains("/")) {
                currPath = currPath.substring(0, currPath.lastIndexOf("/"));
            }
            String[] positons = currPosition.split("/");
            adapter = new FileAdapter(Dao.getInstance(getActivity()).getAllFile("parent_url=?", new String[]{currPath + "/"}), getActivity(),listView);
            listView.setAdapter(adapter);
            try {
                listView.setSelection(Integer.valueOf(positons[positons.length - 1]));
            } catch (Exception ex) {
                Log.i("info", "转换异常216行");
            }
            search_result.setText(currPath.equals(indexPath) ? "网盘" : currPath);
            String[] split = currPath.split("/");
            setTitle(split[split.length-1]);
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
